package com.manager.sensitive.controller;

import com.manager.common.constant.TcpDataType;
import com.manager.common.customer.utils.RemoteExecute;
import com.manager.common.utils.DataTools;
import com.manager.common.utils.DateTools;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.SensitiveWord;
import com.manager.model.SensitiveWordLog;
import com.manager.sensitive.service.SensitiveWordService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sensitiveWord")
public class SensitiveWordController {

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @RequestMapping("/getHitLogPage")
    public PageVO getHitLogPage(SensitiveWordLog model) {
        return sensitiveWordService.getHitLogPage(model);
    }

    @RequestMapping("/getSensitiveWordPage")
    public PageVO getSensitiveWordPage(SensitiveWord model) {
        return sensitiveWordService.getSensitiveWordPage(model);
    }

    @RequestMapping("/addOrUpdate")
    public ResultVO addOrUpdate(SensitiveWord model) {
        try{
            if(StringUtils.isEmpty(model.getId())){
                sensitiveWordService.addSensitiveWord(model);
            }else {
                sensitiveWordService.updateSensitiveWord(model);
            }
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/delete")
    public ResultVO delete(SensitiveWord model) {
        try {
            sensitiveWordService.deleteSensitiveWord(model);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/updateSensitiveWordEnabled")
    public ResultVO updateSensitiveWordEnabled(SensitiveWord model) {
        try {
            sensitiveWordService.updateSensitiveWordEnabled(model);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/issuedSensitiveWords")
    public ResultVO issuedSensitiveWords(){
        try {
            List<SensitiveWord> list = sensitiveWordService.getSensitiveWordList();
            if(list == null || list.size() == 0){
                return ResultVO.failed("当前没有可下发的敏感词数据!");
            }
            //vords
            String url = DataTools.getSystemSettingStringValByKey("issue_instructions_url");
            JSONObject request = new JSONObject();
            request.put("code", TcpDataType.SENSITIVE_WORD.getCode());

            JSONObject params = new JSONObject();
            JSONArray words = new JSONArray();
            for(SensitiveWord sensitiveWord : list){
                JSONObject word = new JSONObject();
                word.put("word", sensitiveWord.getWord());
                words.add(word);
            }
            String serialNumber = DataTools.getSerialNumber();
            params.put("serialNumber", serialNumber);
            params.put("code",1);
            params.put("words", words);
            request.put("content",params);
            RemoteExecute.executeTcpInstruction(url, request.toString());
            DataTools.putWebSessionId(serialNumber);

            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/handleSoft")
    public ResultVO handleSoft(String state){
        try{
            String url = DataTools.getSystemSettingStringValByKey("issue_instructions_url");
            JSONObject request = new JSONObject();
            request.put("code", TcpDataType.SENSITIVE_WORD_SOFT.getCode());
            JSONObject params = new JSONObject();
            String serialNumber = DataTools.getSerialNumber();
            params.put("serialNumber", serialNumber);
            params.put("code",Integer.valueOf(state));
            request.put("content",params);
            ResultVO resultVO = RemoteExecute.executeTcpInstruction(url, request.toString());
            DataTools.putWebSessionId(serialNumber);
            return resultVO;

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }
}
